package com.source.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.source.sys.entity.Popedom;
import com.source.sys.mapper.PopedomMapper;
import com.source.sys.service.PopedomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.source.sys.vo.PopedomShowVo;
import com.source.utils.SysEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZZF
 * @since 2021-02-08
 */
@Service
public class PopedomServiceImpl extends ServiceImpl<PopedomMapper, Popedom> implements PopedomService {
    @Resource
    private PopedomMapper popedomMapper;
    @Override
    public boolean addPopedom(Popedom popedom) {
        int insert = popedomMapper.insert(popedom);
        if(insert > 0){
            //添加成功
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<Popedom> getSonPopedom(String popedomId) {
        List<Popedom> sonPopedom = popedomMapper.getSonPopedom(popedomId);
        sonPopedom.sort(new Comparator<Popedom>() {
            @Override
            public int compare(Popedom o1, Popedom o2) {
                return o1.getSort()-o2.getSort();
            }
        });
        return sonPopedom;
    }

    @Override
    public List<Popedom> getFirstLevelPopedom() {
        List<Popedom> firstLevelPopedom = popedomMapper.getFirstLevelPopedom("p0");
        firstLevelPopedom.sort(new Comparator<Popedom>() {
            @Override
            public int compare(Popedom o1, Popedom o2) {
                return o1.getSort()-o2.getSort();
            }
        });
        return firstLevelPopedom;
    }

    @Override
    public Popedom getPopedomById(String popedomId) {
        return popedomMapper.selectOne(new QueryWrapper<Popedom>().eq("pid",popedomId));
    }

    @Override
    public Popedom getPrePopedomById(String popedomId) {
        return popedomMapper.getPrePopedom(popedomId);
    }

    @Override
    public Popedom getSecondPopedomByName(String pname) {
        return popedomMapper.getSecondPopedomByName((String)SysEnum.POPEDOM_PARENTID.getData(),pname);
    }

    @Override
    public List<PopedomShowVo> getThirdPopedomListByRoleId(String roleId) {
        return popedomMapper.getThirdPopedomByRoleId(roleId);
    }

    @Override
    public boolean checkUriAndMethodByRoleId(String roleId, String uri, String method) {
        //如果最后存在/，把/切掉
        if(uri.lastIndexOf('/') == uri.length() - 1){
            uri = uri.substring(0,uri.lastIndexOf('/'));
        }
        return popedomMapper.checkUriAndMethodByRoleId(roleId,uri,method) != null? true : false;

    }
}
